package com.bops.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;


import com.bops.biz.permission.GrantManager;
import com.bops.dal.order.dataobject.OrderInfoDO;
import com.bops.dal.order.queryobject.OrderInfoQuery;
import com.bops.dal.product.dataobject.CloudDO;
import com.bops.dal.product.dataobject.ProductsDO;
import com.bops.service.CloudService;
import com.bops.service.CommonServiceLocator;
import com.bops.service.OrderService;
import com.bops.service.ProductService;
import com.bops.valve.webuser.BopsWebUser;
import com.bops.web.ViewTools;
import com.yue.commons.seine.web.annotations.ControllerAction;
import com.yue.commons.seine.web.servlet.mvc.AbstractController;
import com.yue.commons.seine.web.servlet.result.Redirect;
import com.yue.commons.seine.web.servlet.result.View;
import com.yue.commons.seine.web.servlet.result.WebResult;

public class OrderController extends AbstractController {

	private CloudService cloudService = CommonServiceLocator.getCloudService();
	private OrderService orderService = CommonServiceLocator.getOrderService();
	private ProductService productSerive = CommonServiceLocator
			.getProductService();
	/**
	 * 审核关单订单
	 * */
	@ControllerAction 
	public WebResult saveClose(Map<String, Object> model,OrderInfoDO orderDO ,Boolean finish){
		if(finish){
			orderDO.setStatus(orderService.CLOSE);
		}else{
			orderDO.setStatus(orderService.OVER);
		}
        orderService.update(orderDO);
        OrderInfoQuery queryOrder = new OrderInfoQuery();
		return searchOrder(model, queryOrder);
	}
	
	/**
	 * 审核已付款订单
	 * */
	@ControllerAction 
	public WebResult savepaying(Map<String, Object> model,OrderInfoDO orderDO ,Boolean finish){
		if(finish){
			orderDO.setStatus(orderService.CLOSE);
		}else{
			orderDO.setStatus(orderService.OVER);
		}
        orderService.update(orderDO);
        OrderInfoQuery queryOrder = new OrderInfoQuery();
		return searchOrder(model, queryOrder);
	}
	
	/**
	 * 审核待处理订单
	 * */
	@ControllerAction 
	public WebResult savechecking(Map<String, Object> model,OrderInfoDO orderDO ,Boolean finish){
		if(finish){
			orderDO.setStatus(orderService.PAIED);
		}else{
			orderDO.setStatus(orderService.OVER);
		}
        orderService.update(orderDO);
        OrderInfoQuery queryOrder = new OrderInfoQuery();
		return searchOrder(model, queryOrder);
	}
	
	/**
	 * 1.生成一条用户选择的云主机信息插入表 2.生成的新数据的ID,生成一个新的订单
	 * 
	 * */
	@ControllerAction
	public WebResult createOrder(Map<String, Object> model, CloudDO cloudDO,
			OrderInfoDO orderDO) {
		BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
		if (bopsWebUser != null) {
			Integer id = cloudService.insert(cloudDO); // 记录下云服务器配置
			if (id != null) {
				ProductsDO product = new ProductsDO();
				product.setCloud_id(id);
				product.setProduct_no("云主机");
				product.setPrice(orderDO.getAmount());
				product.setLicences(cloudDO.getYear());
				// 根据配置生成一种产品插入产品表
				Integer pid = productSerive.insertProduct(product);
				if (pid == null) {
					model.put("status", "购买失败了，请联系客服");
				} else {
					String orderNO = orderNO(pid);
					orderDO.setOrderNO(orderNO);
					orderDO.setProduct_id(pid);
					orderDO.setPayuser(bopsWebUser.getMemberId());
					Integer r = orderService.insert(orderDO);
				    model.put("status", "编号"+orderDO.getOrderNO()+" 订单已生成");
				}

			}
			model.put("pagename", "订单信息");
			OrderInfoQuery query = new OrderInfoQuery();
			query.setPageSize(Integer.MAX_VALUE);
			query.setPayuser(bopsWebUser.getMemberId());
			List<OrderInfoDO> orderList = orderService.listOrder(query);
			model.put("orderList", orderList);
		} else {
			return new Redirect("/login/index.htm");
		}
		return new View("/orderUser/order.htm");
	}

	/**
	 * 生成订单编号 日期+产品ID+id
	 * */
	public String orderNO(int id) {
		Date date = new Date();
		String orderNo = ViewTools.formatSimpleDate(date).replaceAll("-", "");
		Integer maxid = orderService.maxOrderID() + 1;
		orderNo = orderNo + id + maxid;
		return orderNo;
	}

	/**
	 * 生成订单
	 * */
	@ControllerAction
	public WebResult order(Map<String, Object> model, OrderInfoDO orderDO) {

		BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
		if (bopsWebUser != null) {

			if (orderDO.getProduct() != null) {
				String orderNO = orderNO(orderDO.getProduct_id());
				orderDO.setOrderNO(orderNO);
				orderDO.setPayuser(bopsWebUser.getMemberId());
				Integer r = orderService.insert(orderDO);
				model.put("status", "编号" + orderDO.getOrderNO() + " 订单已生成");
			}
			model.put("pagename", "订单信息");
			OrderInfoQuery query = new OrderInfoQuery();
			query.setPageSize(Integer.MAX_VALUE);
			query.setPayuser(bopsWebUser.getMemberId());
			List<OrderInfoDO> orderList = orderService.listOrder(query);
			model.put("orderList", orderList);
		} else {
			return new Redirect("/login/index.htm");
		}
		return new View("/orderUser/order.htm");
	}

	/**
	 * 修改订单
	 * */
	@ControllerAction
	public WebResult editOrderForm(Map<String, Object> model,
			OrderInfoDO orderDO) {
		return new View("/order/editOrder.htm");
	}

	/**
	 * 审核订单
	 * */
	@ControllerAction
	public WebResult viewOrderForm(Map<String, Object> model, Integer id,
			String action) {
		OrderInfoDO orderDO = new OrderInfoDO();
		if (id == null) {
			model.put("errorStatus", "编号是" + id + "记录是错误的!");
			return null;
		}
		orderDO = orderService.getById(id);
		if (orderDO == null) {
			model.put("errorStatus", "没有编号是" + id + "的记录!");
			return null;
		}
		model.put("orderDO", orderDO);

		if ("view".equals(action)) {
			return new View("/order/viewOrder.htm");
		} else if ("check".equals(action)) {
			if (GrantManager.hasPermission("order_check_role")) {
				model.put("order_check_role", "true");
				BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
				model.put("username", bopsWebUser.getName());
				return new View("/order/checkOrder.htm");
			}

		}else if("pay".equals(action)){
			if (GrantManager.hasPermission("order_check_role")) {
				model.put("order_check_role", "true");
				BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
				model.put("username", bopsWebUser.getName());
				return new View("/order/payOrder.htm");
			}
			
		}else if("close".equals(action)){
			if (GrantManager.hasPermission("order_check_role")) {
				model.put("order_check_role", "true");
				BopsWebUser bopsWebUser = BopsWebUser.getCurrentUser();
				model.put("username", bopsWebUser.getName());
				return new View("/order/closeOrder.htm");
			}
			
		}else if ("edit".equals(action)) {
			return new View("/order/editOrder.htm");
		}
		return new View("/order/viewOrder.htm");
	}

	/**
	 * 查询订单
	 * */
	@ControllerAction
	public WebResult searchOrder(Map<String, Object> model,
			OrderInfoQuery queryOrder) {
		List<OrderInfoDO> orderList = orderService.listOrder(queryOrder);
		model.put("orderList", orderList);
		model.put("orderDO", queryOrder);
		model.put("pagniation", orderList);
		if (GrantManager.hasPermission("order_check_role")) {
			model.put("order_check_role", "true");
		}
		return new View("/order/searchOrder.htm");
	}

}
